// 上报模块
/*----------依赖模块--------- */
const express = require('express');
const router = express.Router();
const db = require('../db/db');
const back = require('../utils/back');
const md5 = require('md5');
const middle = require('../middle/auth');
const dayjs = require('dayjs');
const token = require('../controller/token')

/*----------API方法---------- */

const report = {}

//上传
report.upload = async (req, res) => {
    const { user_id } = req.userInfo;
    const params = req.body;

    // 参数是否齐全
    if (!params.report_reason || !params.report_image || !params.licenseplate) {
        // 如果参数不全，要返回给客户端一个内容
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '上传的参数不全'))
        return;
    }

    //查找上报人
    const selectUser = await db.query('select * from user where user_id=?', [user_id])
    if (selectUser === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败,上报人'));
        return;
    }
    //  判断手机号
    if (selectUser.length < 1) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '该用户不存在'));
        return;
    }

    // 对时间格式进行处理
    const d = params.time ? dayjs(params.time) : dayjs();
    const report_time = d.format('YYYY-MM-DD HH:mm:ss');
    // 插入新的举报信息
    const state = await db.query('insert into contactrecord (reporter_id,licenseplate,report_time,is_contacted,is_vip,report_reason,report_image) values(?,?,?,?,?,?,?)',
        [user_id, params.licenseplate, report_time, 0, selectUser[0].is_vip, params.report_reason, params.report_image]);
    if (state === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '插入失败'));
        return;
    }

    //查找被处理人
    let processedCard = await db.query('select * from carowner where licenseplate ', [params.licenseplate])
    if (processedCard === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    if (processedCard.length < 1) {
        // res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '该用户未注册我软件'));
        const publicCard = await db.query('select * from publiccard where licenseplate=?', [params.licenseplate])
        if (selectUser === false) {
            res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
            return;
        }
        processedCard = publicCard;
    }
    const order = {
        selectUser: selectUser,
        processedCard: processedCard
    }

    res.send(back.Data(order))
    return;
}

//紧急上传
report.emergency = async (req, res) => {
    const { user_id } = req.userInfo;
    const params = req.body;


    // 参数是否齐全
    if (!params.user_location|| !params.report_image) {
        // 如果参数不全，要返回给客户端一个内容
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '上传的参数不全'))
        return;
    }

    //查找上报人
    const selectUser = await db.query('select * from user where user_id=?', [user_id])
    if (selectUser === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败,上报人'));
        return;
    }
    // 对时间格式进行处理
    const d = params.time ? dayjs(params.time) : dayjs();
    const report_time = d.format('YYYY-MM-DD HH:mm:ss');

    const state = await db.query('insert into emergency (user_id,user_phone,user_location,report_time,report_image) values(?,?,?,?,?)',
        [user_id, selectUser[0].phone_number, params.user_location, report_time,params.report_image]);
    if (state === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '插入失败'));
        return;
    }

    const emergency = await db.query('select * from emergency where id=?', [state.insertId])
    if (emergency === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查找失败'));
        return;
    }
    res.send(back.Data(emergency))
    return;
}
//获得我的举报
report.getReport = async (req, res) => {
    const { user_id } = req.userInfo;
    const params = req.query;

    const contactre = await db.query('select * from contactrecord where reporter_id=?', [user_id])
    if (contactre === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    res.send(back.Data(contactre));
}

//得到我的被举报
report.getmyRepored = async (req, res) => {
    const { user_id } = req.userInfo;
    const params = req.query;

    const contactre = await db.query('select * from contactrecord where reported_id=?', [user_id])
    if (contactre === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    res.send(back.Data(contactre));
}

//得到被举报人
report.raised = async (req, res) => {
    const params = req.query;

    const myuser = await db.query('select * from user where licenseplate=?', [params.licenseplate])
    if (myuser === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    if (myuser.length < 1) {
        const owners = await db.query('select * from publiccard where licenseplate=?', [params.licenseplate])
        if (owners === false) {
            res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
            return;
        }
        res.send(back.Data(owners))
        return;
    }
    res.send(back.Data(myuser))
    return;
}

//跟踪举报
report.dispose = async (req, res) => {
    const { user_id } = req.userInfo
    const params = req.query;

    const myraised = await db.query('select * from contactrecord where id=? ', [params.id])
    if (myraised === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    const update = await db.query('update contactrecord set know=? where id=?', [1, params.id])
    if (update === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '修改失败'));
        return;
    }
    res.send(back.Data(update))
    return;
}

//完成举报
report.finish = async (req, res) => {
    const { user_id } = req.userInfo
    const params = req.query;

    const myraised = await db.query('select * from contactrecord where id=? and is_contacted=? ', [params.id, 0])
    if (myraised === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }

    const update = await db.query('update contactrecord set is_contacted=? where id=?', [1, params.id])
    if (update === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '修改失败'));
        return;
    }
    res.send(back.Data(update))
    return;
}

/*---------路由---------- */
// 注册
router.post('/upload', middle.userAuth, report.upload)
router.get('/getReport', middle.userAuth, report.getReport)
router.get('/raised', middle.userAuth, report.raised)
router.get('/dispose', middle.userAuth, report.dispose)
router.get('/finish', middle.userAuth, report.finish)
router.get('/getmyRepored', middle.userAuth, report.getmyRepored)
router.post('/emergency', middle.userAuth, report.emergency)


/*-------暴露----------- */
module.exports = router;